package com.umeox.watch.moto.launcher.ui;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.HeartRateAdapter;

import java.util.List;

/**
 * Created by Rambo 3/26/21
 * Describe:
 */
public class HeartRateHistoryActivity extends ThemeAppCompatActivity {

    private static final int REQUEST_CODE = 101;

    private TextView dateTv;
    private TextView minHeartTv;
    private TextView maxHeartTv;
    private TextView averageHeartTv;
    private RecyclerView mHeartRateRv;
    private HeartRateAdapter heartRateAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heartrate_history);

        initView();
        initData();
    }

    private void initView() {
        dateTv = findViewById(R.id.tv_date);
        minHeartTv = findViewById(R.id.tv_min_heart);
        maxHeartTv = findViewById(R.id.tv_max_heart);
        averageHeartTv = findViewById(R.id.tv_average_heart);
        mHeartRateRv = findViewById(R.id.heart_rate_rv);
    }

    private void initData() {

        List<ContentValues> values = DataProvider.getHeartRatesByLimit(10);
        if (values != null && values.size() > 0) {
            ContentValues contentValues = values.get(0);
            dateTv.setText(DateUtils.formatDateTime(contentValues.getAsLong(DataProvider.HeartRateProvider.MEASURE_TIME), "HH:mm,MM-dd"));
            minHeartTv.setText(contentValues.getAsString(DataProvider.HeartRateProvider.MIN_VALUE));
            maxHeartTv.setText(contentValues.getAsString(DataProvider.HeartRateProvider.MAX_VALUE));
            averageHeartTv.setText(contentValues.getAsString(DataProvider.HeartRateProvider.AVERAGE_VALUE));

            values.remove(0);
            heartRateAdapter = new HeartRateAdapter(this, values);
            mHeartRateRv.setLayoutManager(new LinearLayoutManager(this) {
                @Override
                public boolean canScrollVertically() {
                    return true;
                }
            });
            mHeartRateRv.setAdapter(heartRateAdapter);
        }
    }


    public void startDetect(View view) {

        Intent intent = new Intent(HeartRateHistoryActivity.this, HeartRateDetectActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            int[] datas = data.getIntArrayExtra("data");
            if (datas != null && datas.length == 3) {
                dateTv.setText(DateUtils.formatDateTime(System.currentTimeMillis(), "HH:mm,MM-dd"));
                minHeartTv.setText(String.valueOf(datas[0]));
                maxHeartTv.setText(String.valueOf(datas[1]));
                averageHeartTv.setText(String.valueOf(datas[2]));

                List<ContentValues> values = DataProvider.getHeartRatesByLimit(10);

                if (values.size() > 1) {
                    values.remove(0);
                    if(heartRateAdapter != null){
                        heartRateAdapter.setDataList(values);
                    }
                }
            }
        }
    }
}
